#include "hashTableSortedVector.h"


//Constructor
hashTableSortedVector::hashTableSortedVector()
{
	hashT = new hashTable();
	sortedVector = new sortedVectorByF();
}


//Return true if the hash table is empty
bool hashTableSortedVector::empty()
{
	return hashT->empty();
}


//Allocates memory for the hash table and the sorted vector
void hashTableSortedVector::allocate(int width, int height)
{
	hashT->allocate(width, height);
	sortedVector->allocate(width, height);
}


//Free the memory allocated in both structures
void hashTableSortedVector::freeNodes()
{
	hashT->freeNodes();
	sortedVector->freeNodes();
	delete hashT;
	delete sortedVector;
}


//Remove the node from the hash table and the vector and add it to them again
void hashTableSortedVector::modifyNode(Node** item)
{
	Node* node = *item;

	//Remove the node
	hashT->remove(item);
	sortedVector->remove((sortedVector->findNode(node->getX(), node->getY())).first);

	//Add the node
	hashT->addNode(node);
	sortedVector->addNode(node);
}


//Look for a node with coordinates x, y in the hash table
//If found, returns a pointer to the node and a pointer to its position in the array
std::pair<Node**, Node*> hashTableSortedVector::findNode(int x, int y)
{
	return hashT->findNode(x, y);
}


//Returns the node with lowest cost in the sorted vector
Node* hashTableSortedVector::selectNodeLowestScore()
{
	//Select the node with lowest cost
	Node* node = sortedVector->selectNodeLowestScore();

	//Remove this node from the hash table
	hashT->remove((hashT->findNode(node->getX(), node->getY())).first);

	return node;
}


//Remove the pointer to node stored in *item from the hash table and the sorted vector
void hashTableSortedVector::remove(Node** item)
{
	Node* node = *item;

	//Remove the node
	hashT->remove(item);
	sortedVector->remove((sortedVector->findNode(node->getX(), node->getY())).first);
}


//Add the node to the hash table and the sorted vector
void hashTableSortedVector::addNode(Node* node)
{
	hashT->addNode(node);
	sortedVector->addNode(node);
}